#pragma once

/********************************************************************************
MIT License

Copyright(c) 3dRudder 2017

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
********************************************************************************/


#define _3DRUDDER_SDK_STATIC
#include "3dRudderSDK.h"

#include "SharedMemory.h"

class CSISharedData
{
public:

	enum AxisValue
	{
		avDefault,
		avLeftRight,
		avBackwardForward,
		avUpDown,
		avRotationLeftRight
	};

	enum HalfAxisValue
	{
		havDefault,
		havLeft,
		havRight,
		havBackward,
		havForward,
		havUp,
		havDown,
		havRotationLeft,
		havRotationRight
	};

	// right-handed system
	// +y is up
	// +x is to the right
	// -z is going away from you
	// Distance unit is  meters
	struct Matrix34
	{
		float m[3][4];
	};

	CSISharedData()
	{
		m_nPadIndex[0] = 0;
		m_nPadIndex[1] = 0;

		ns3dRudder::CurveArray Curve;
		m_Curve[ns3dRudder::CurveXAxis] = *Curve.GetCurve(ns3dRudder::CurveXAxis);
		m_Curve[ns3dRudder::CurveYAxis] = *Curve.GetCurve(ns3dRudder::CurveYAxis);
		m_Curve[ns3dRudder::CurveZAxis] = *Curve.GetCurve(ns3dRudder::CurveZAxis);
		m_Curve[ns3dRudder::CurveZRotation] = *Curve.GetCurve(ns3dRudder::CurveZRotation);
		m_bNonSymPitch = true;
		m_fHeightOffset = 0;
		m_bCalibrateOnInUse = true;

		m_bActivateCtrls=false;
		for (int i = 0; i < 4; i++)
			m_nAxis[i] = avDefault;
		for (int i = 0; i < 2; i++)
			m_nHalfAxis[i] = havDefault;

		m_bActivateVRLocomotion=false;
		m_bSmoothMode = false;
		for (int i = 0;i < 4;i++)
		{
			m_fSpeed[i] = 1.0f;
			m_fSmoothValue[i] = 0.15f;
		}
	}

	ns3dRudder::Curve m_Curve[4];
	bool m_bNonSymPitch;
	bool m_bCalibrateOnInUse;
	float m_fHeightOffset;

	bool m_bActivateCtrls;
	int m_nPadIndex[2];

	AxisValue m_nAxis[4];
	HalfAxisValue m_nHalfAxis[2];

	bool m_bActivateVRLocomotion;
	float m_fSpeed[4];
	bool m_bSmoothMode;
	float m_fSmoothValue[4];


	bool Load(wchar_t *pPathName)
	{
		bool bRet = false;
		FILE *pFile = _wfopen(pPathName, L"rt");
		if (pFile)
		{
			int nVal1;
			int nVal2;
			int nVal3;
			int nVal4;
			int nVal5;
			int nVal6;
			int nVal7;

			float fVal1;
			float fVal2;
			float fVal3;
			float fVal4;

			int nNbVal = fscanf(pFile, "%d\n", &nVal1);
			if (nVal1 == 6)
			{
				int i = 0;
				nNbVal = fscanf(pFile, "%d,%d,%d,%d,%d,%d,%d\n", &nVal1, &nVal2, &nVal3, &nVal4, &nVal5, &nVal6, &nVal7);
				m_bActivateCtrls = !!nVal1;
				m_nAxis[0] = (AxisValue)nVal2;
				m_nAxis[1] = (AxisValue)nVal3;				
				m_nAxis[2] = (AxisValue)nVal5;
				m_nAxis[3] = (AxisValue)nVal6;
				m_nHalfAxis[0] = (HalfAxisValue)nVal4;
				m_nHalfAxis[1] = (HalfAxisValue)nVal7;

				nNbVal = fscanf(pFile, "%d,%d,%f,%f,%f,%f\n", &nVal1, &nVal2, &fVal1, &fVal2, &fVal3, &fVal4  );
				m_bActivateVRLocomotion = !!nVal1;
				m_fSpeed[0] = fVal1;
				m_fSpeed[1] = fVal2;
				m_fSpeed[2] = fVal3;
				m_fSpeed[3] = fVal4;
				m_bSmoothMode = !!nVal2;								

				for (i = 0; i < 4; i++)
				{
					nNbVal = fscanf(pFile, "%f\n", &fVal1);
					m_fSmoothValue[i] = fVal1;
				}

				nNbVal = fscanf(pFile, "%d,%d,%f\n", &nVal1, &nVal2, &fVal1);
				m_bNonSymPitch = !!nVal1;
				m_bCalibrateOnInUse = !!nVal2;
				m_fHeightOffset = fVal1;


				for (i = 0; i < 4; i++)
				{
					nNbVal = fscanf(pFile, "%f,%f,%f,%f\n", &fVal1, &fVal2, &fVal3, &fVal4);
					m_Curve[i].m_fDeadZone = fVal1;
					m_Curve[i].m_fxSat = fVal2;
					m_Curve[i].m_fExp = fVal3;
					m_Curve[i].m_fyMax = fVal4;
				}
				bRet = true;
			}
			fclose(pFile);
		}
		return bRet;
	}

	bool Save(wchar_t *pPathName)
	{
		bool bRet = false;
		FILE *pFile = _wfopen(pPathName, L"wt");
		if (pFile)
		{
			int i;
			fprintf(pFile, "6\n");
			fprintf(pFile, "%d, %d, %d, %d, %d, %d, %d\n", m_bActivateCtrls, m_nAxis[0], m_nAxis[1], m_nHalfAxis[0], m_nAxis[2], m_nAxis[3], m_nHalfAxis[1]);

			fprintf(pFile, "%d, %d, %f, %f, %f, %f\n", m_bActivateVRLocomotion, m_bSmoothMode, m_fSpeed[0], m_fSpeed[1], m_fSpeed[2], m_fSpeed[3] );
			for (i = 0; i < 4; i++)
				fprintf(pFile, "%f\n", m_fSmoothValue[i]);

			fprintf(pFile, "%d, %d, %f\n", m_bNonSymPitch, m_bCalibrateOnInUse, m_fHeightOffset);
			for (i = 0; i < 4; i++)
			{
				fprintf(pFile, "%f, %f, %f, %f\n", m_Curve[i].m_fDeadZone, m_Curve[i].m_fxSat, m_Curve[i].m_fExp, m_Curve[i].m_fyMax);
			}

			fclose(pFile);
			bRet = true;
		}
		return bRet;
	}
};

class CSISharedDataDebug
{
public:

	class CVector
	{
	public:
		CVector()
		{
			m_fx = 0.0f;
			m_fy = 0.0f;
			m_fz = 0.0f;
			m_fw = 0.0f;
		}

		double m_fx;
		double m_fy;
		double m_fz;
		double m_fw;
	};

	CVector m_RotationInitial;
	CVector m_PositionInitial;
	CVector m_TranslationXYZAngleW;
	CVector m_SmoothSpeed;
	double m_fDeltaTime;
};



typedef enum :uint32_t
{
	Reset,
	Calibration,

}CSiMessageeType;



class CSI_MessageReset : public CSI_Message
{
public:
	CSI_MessageReset()
	{
		m_nLen = sizeof(CSI_MessageReset);
		m_nType = CSiMessageeType::Reset;
	}
	bool m_bReset;
};

class CSI_MessageCalibration : public CSI_Message
{
public:
	CSI_MessageCalibration()
	{
		m_nLen = sizeof(CSI_MessageCalibration);
		m_nType = CSiMessageeType::Calibration;
		m_bCalib = false;
	}
	CSISharedData::Matrix34 m_matrix;
	bool m_bCalib;
};

